//
//  ImageDB.m
//  Comma
//
//  Created by liuchang on 15/1/26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ImageDB.h"
#import "DB.h"

@implementation ImageDB

+ (void)insertIntoActWithDic:(NSDictionary *)imaDic
{
    sqlite3 * dbpoint = [DB openDB];
//    NSString * sqlStr = [NSString stringWithFormat:@"INSERT INTO Expression (code,name,url,description) VALUES ('%@','%@','%@','%@')", [imaDic objectForKey:@"code"], [imaDic objectForKey:@"name"], [imaDic objectForKey:@"url"], [imaDic objectForKey:@"description"]];
    NSData * imaData = [imaDic objectForKey:@"imaData"];
//    int result = sqlite3_exec(dbpoint, [sqlStr UTF8String], nil, nil, nil);
//    if (result == SQLITE_OK) {
//        NSLog(@"成功");
//    }
//    else {
//        NSLog(@"失败");
//    }
    const char* sqliteQuery = [[NSString stringWithFormat:@"INSERT INTO Expression (code,name,url,description,imaData) VALUES ('%@','%@','%@','%@',?)", [imaDic objectForKey:@"code"], [imaDic objectForKey:@"name"], [imaDic objectForKey:@"url"], [imaDic objectForKey:@"description"]] UTF8String];
    sqlite3_stmt* statement;
    if( sqlite3_prepare_v2(dbpoint, sqliteQuery, -1, &statement, NULL) == SQLITE_OK )
    {
        sqlite3_bind_blob(statement, 1, [imaData bytes], [imaData length], SQLITE_TRANSIENT);
        sqlite3_step(statement);
    }
    sqlite3_finalize(statement);
}

+ (void)deleteFromActWithModelName:(NSString *)name
{
    sqlite3 * dbpoint = [DB openDB];
    NSString * sqlStr = [NSString stringWithFormat:@"DELETE FROM Expression WHERE name='%@'", name];
    int result = sqlite3_exec(dbpoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"成功");
    }
    else {
        NSLog(@"失败");
    }
}

+ (NSArray *)searchAllDic
{
    sqlite3 * dbpoint = [DB openDB];
    sqlite3_stmt * stmt = nil;
    NSString * sqlStr = @"SELECT * FROM Expression";
    int result = sqlite3_prepare_v2(dbpoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSMutableArray * arr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *code = sqlite3_column_text(stmt, 0);
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *url = sqlite3_column_text(stmt, 2);
            const unsigned char *description = sqlite3_column_text(stmt, 3);
//            const unsigned char *imaData = sqlite3_column_text(stmt, 4);
            NSString * str_code = [NSString stringWithUTF8String:(const char *)code];
            NSString * str_name = [NSString stringWithUTF8String:(const char *)name];
            NSString * str_url = [NSString stringWithUTF8String:(const char *)url];
            NSString * str_description = [NSString stringWithUTF8String:(const char *)description];
            
            
            
//            NSString * str_imaData = [NSString stringWithUTF8String:(const char *)imaData];
//            NSData * _imaData = [str_imaData dataUsingEncoding:NSUTF8StringEncoding];
            
            NSData* data = nil;
            NSString* sqliteQuery = [NSString stringWithFormat:@"SELECT * FROM Expression WHERE name = '%@'", str_name];
            sqlite3_stmt* statement;
            
            if( sqlite3_prepare_v2(dbpoint, [sqliteQuery UTF8String], -1, &statement, NULL) == SQLITE_OK )
            {
                if( sqlite3_step(statement) == SQLITE_ROW )
                {
                    int length = sqlite3_column_bytes(statement, 4);
                    data = [NSData dataWithBytes:sqlite3_column_blob(statement, 4) length:length];
                }
            }
            
            // Finalize and close database.  
            sqlite3_finalize(statement);
            
            
            
            
            
            NSDictionary * imaDic = @{@"code":str_code, @"name":str_name, @"url":str_url, @"description":str_description, @"imaData":data};
            [arr addObject:imaDic];
        }
    }
    
    sqlite3_finalize(stmt);
    
    return arr;
}

+ (NSDictionary *)searchModelWithName:(NSString *)name
{
    sqlite3 * dbpoint = [DB openDB];
    sqlite3_stmt * stmt = nil;
    NSString * sqlStr = [NSString stringWithFormat:@"SELECT * FROM activityDb WHERE name='%@'", name];
    int result = sqlite3_prepare_v2(dbpoint, [sqlStr UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *code = sqlite3_column_text(stmt, 0);
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *url = sqlite3_column_text(stmt, 2);
            const unsigned char *description = sqlite3_column_text(stmt, 3);
            const unsigned char *imaData = sqlite3_column_text(stmt, 4);
            NSString * str_code = [NSString stringWithUTF8String:(const char *)code];
            NSString * str_name = [NSString stringWithUTF8String:(const char *)name];
            NSString * str_url = [NSString stringWithUTF8String:(const char *)url];
            NSString * str_description = [NSString stringWithUTF8String:(const char *)description];
            NSString * str_imaData = [NSString stringWithUTF8String:(const char *)imaData];
            NSData * _imaData = [[NSData alloc] initWithBase64EncodedString:str_imaData options:NSDataBase64DecodingIgnoreUnknownCharacters];
            NSDictionary * imaDic = @{@"code":str_code, @"name":str_name, @"url":str_url, @"description":str_description, @"imaData":_imaData};
            sqlite3_finalize(stmt);
            return imaDic;
        }
    }
    
    sqlite3_finalize(stmt);
    
    return nil;
}

@end
